import ReactDOMClient from 'react-dom/client';
import Message from "../../components/Message/Message";
const message = {
  dom: null,
  success({content, duration = 2000}) {
    // 创建一个dom
    this.dom = document.createElement('div')
    // 把这个dom放在节点中
    document.body.appendChild(this.dom);
    const root = ReactDOMClient.createRoot(this.dom)
    root.render(<Message content={content} duration={duration} type='success' />)
    setTimeout(() => {
      this.dom.parentNode && this.dom.parentNode.removeChild(this.dom)
    }, duration)
  },
  error({content, duration = 2000}) {
      this.dom = document.createElement('div');
      document.body.appendChild(this.dom);
      const root = ReactDOMClient.createRoot(this.dom)
      root.render(<Message content={content} duration={duration} type='error' />)
      setTimeout(() => {
        this.dom.parentNode && this.dom.parentNode.removeChild(this.dom)
      }, duration)
  },
  warning({content, duration = 2000}) {
      this.dom = document.createElement('div');
      document.body.appendChild(this.dom);
      const root = ReactDOMClient.createRoot(this.dom)
      root.render(<Message content={content} duration={duration} type='warning' />)
      setTimeout(() => {
        this.dom.parentNode && this.dom.parentNode.removeChild(this.dom)
      }, duration)
  },
  info({content, duration = 2000}) {
      this.dom = document.createElement('div');
      document.body.appendChild(this.dom);
      const root = ReactDOMClient.createRoot(this.dom)
      root.render(<Message content={content} duration={duration} type='info' />)
      setTimeout(() => {
        this.dom.parentNode && this.dom.parentNode.removeChild(this.dom)
      }, duration)
  }
};

export default message;